package com.etheart.music.service;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.etheart.framework.core.dao.CrudService;
import com.etheart.framework.core.dao.QueryService;
import com.etheart.model.entity.Singer;

@Service("music_SingerService")
public class SingerServiceImp implements SingerService {
	@Autowired
	@Qualifier("etheart_QueryService")
	private QueryService queryService;

	@Autowired
	@Qualifier("etheart_CrudService")  
	private CrudService crudService;
	
	/* (non-Javadoc)
	 * @see com.etheart.lyric.music.service.SingerService#findAll()
	 */
	@Override
	public List<Singer> findAll(){
		return queryService.findAll(Singer.class);
	}
	
	/* (non-Javadoc)
	 * @see com.etheart.lyric.music.service.SingerService#addSinger(com.etheart.module.entity.Singer)
	 */
	@Override
	@Transactional
	public void addSinger(Singer Singer){
		crudService.create(Singer);
	}
	/* (non-Javadoc)
	 * @see com.etheart.lyric.music.service.SingerService#updateSinger(com.etheart.module.entity.Singer)
	 */
	@Override
	@Transactional
	public void updateSinger(Singer Singer){
		crudService.update(Singer);
	}
	
	/* (non-Javadoc)
	 * @see com.etheart.lyric.music.service.SingerService#deleteSinger(com.etheart.module.entity.Singer)
	 */
	@Override
	@Transactional
	public void deleteSinger(Singer Singer){
		crudService.delete(Singer);
	}

	@Override
	public Singer findById(Long singerId) {
		return queryService.findById(Singer.class,singerId);
	}

	@Override
	public Singer findByNetId(String netId) {
		DetachedCriteria criteria=DetachedCriteria.forClass(Singer.class).add(Restrictions.eq("netId", netId+""));
		List<Singer> singers=queryService.findListByDetachedCriteria(criteria);
		if (singers.size()>0) {
			return singers.get(0);
		}
		return null;
	}

	@Override
	public long getSingerCount() {
		return queryService.getRowCount(Singer.class);
	}

	@Override
	public Session openSession() {
		HibernateTemplate template=(HibernateTemplate) crudService.getDelegate();
		return template.getSessionFactory().openSession();
	}

	public Singer findByFromUrl(String fromUrl) {
		Singer singer = new Singer();
		//singer.setFromUrl(fromUrl);
		List<Singer> singers = queryService.findByExample(singer);
		if (singers.size()>0) {
			return singers.get(0);
		}
		return null;
	}

	
	
}
